<?php
echo $this->Session->flash();
App::Import('model', 'Call');
$tituloConvocatoria = "";
?>
<script>
    $(function() {
        $( ".calendario" ).datepicker();
        $( "#tabs" ).tabs();
        
        //Change these values to style your modal popup
        var align = 'center';									//Valid values; left, right, center
        var top = 100; 											//Use an integer (in pixels)
        var width = 700; 										//Use an integer (in pixels)
        var padding = 10;										//Use an integer (in pixels)
        var backgroundColor = '#FFFFFF'; 						//Use any hex code
        var source = 'demo.html'; 								//Refer to any page on your server, external pages are not valid e.g. http://www.google.co.uk
        var borderColor = '#333333'; 							//Use any hex code
        var borderWeight = 4; 									//Use an integer (in pixels)
        var borderRadius = 5; 									//Use an integer (in pixels)
        var fadeOutTime = 300; 									//Use any integer, 0 = no fade
        var disableColor = '#666666'; 							//Use any hex code
        var disableOpacity = 40; 								//Valid range 0-100
        var loadingImage = 'lib/release-0.0.1/loading.gif';		//Use relative path from this page
			
        //This method initialises the modal popup
        $(".modal").click(function() {
            
            source=this.title;
            modalPopup(align, top, width, padding, disableColor, disableOpacity, backgroundColor, borderColor, borderWeight, borderRadius, fadeOutTime, source, loadingImage);
        });
		
        //This method hides the popup when the escape key is pressed
        $(document).keyup(function(e) {
            if (e.keyCode == 27) {
                closePopup(fadeOutTime);
            }
        });
        
        
        
    });
</script>

<script type="text/javascript">
    // increase the default animation speed to exaggerate the effect
           
    $(document).ready(function() {	
                 
        $('.tabla').dataTable({
            "bPaginate": false       
                       
        });
                
    });
    
    
</script>
<div class="paging">

</div>

<div>
    <table cellpadding="0" cellspacing="0" id="tabla">
        <thead>
            <tr>
                <th><?php echo ('Proyecto'); ?></th>
                <th><?php echo ('Convocatoria'); ?></th>
                <th><?php echo ('Fecha pago programado'); ?></th>
                <th><?php echo ('Monto'); ?></th>
                <th><?php echo ('Tipo'); ?></th>
                <th><?php echo ('Días pasados'); ?></th>
                <th><?php echo ('Ver observaciones'); ?></th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($payments as $payment): ?>
                <?php
                $cambio = false;
                $error = false;
                $fechaProgramacion = strtotime($payment['Payment']['fecha_pago_programado']);
                $fechaPagoReal = strtotime($payment['Payment']['fecha_pago_real']);
                $fechaLimiteInferior = strtotime("0000-00-00");
                //time() nos da el tiempo actual.
                if (time() > $fechaProgramacion) {
                    $error = true;
                    if ($fechaPagoReal != $fechaLimiteInferior) {
                        $error = false;
                    }
                }
                //Busco el nombre de la convocatoria
                $call = new Call();
                $call->recursive = -1;
                $convocatoria = $call->find('first', array('conditions' => array('Call.id' => $payment['Proyect']['call_id']), 'fields' => array('Call.id', 'Call.nombre')));

                if ($tituloConvocatoria != $convocatoria['Call']['nombre']) {
                    $tituloConvocatoria = $convocatoria['Call']['nombre'];
                    $cambio = true;
                }
                ?>


                <?php if ($cambio): ?>
                    <tr>
                        <td style='background-color: #878382;' colspan = "7" ><b><?php echo ("CONVOCATORIA " . $tituloConvocatoria ) ?>&nbsp;</b></td>
                    </tr>
                <?php endif; ?>

                <tr>
                    <td <?php echo ($error ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8;'") ?>>
                        <?php echo ($payment['Proyect']['codigo']) ?>&nbsp;</td>
                    <td <?php echo ($error ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8;'") ?>>
                        <?php echo ($convocatoria['Call']['nombre']) ?>&nbsp;</td>
                    <td <?php echo ($error ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8;'") ?>>
                        <?php echo ($payment['Payment']['fecha_pago_programado']); ?>&nbsp;</td>
                    <td <?php echo ($error ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8;'") ?>>
                        <?php echo "$ " . number_format($payment['Payment']['monto'], 0, ',', '.'); ?>&nbsp;</td>
                    <td <?php echo ($error ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8;'") ?>>
                        <?php echo ($payment['Payment']['tipo']); ?>&nbsp;</td>
                    <td <?php echo ($error ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8;'") ?>>
                        <?php echo round(((time() - $fechaProgramacion) / 86400) - 1); ?>&nbsp;</td>
                    <td <?php echo ($error ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8;'") ?>>
                        <?php
                        echo $html->image('ver.png', array('width' => '30', 'heigth' => '30', 'alt' => 'Ver', 'class' => 'modal', 'title' => $this->Html->url(array('controller' => 'Payments', 'action' => 'view', $payment['Payment']['id']))));
                        ?>
                    </td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>
